<?php
class DB_SQL {
    var $Name = "";
    var $Host = "localhost";
    var $User = "";
    var $Pass = "";
    var $pconnect = false;
    var $link_id = 0;
    var $query_id = 0;
    var $sql = "";
    var $count = 0;

    private static $msqlicnct = NULL;
    
    private function __construct() {}

    private function __clone() {}

    private function __wakeup() {} 

    public static function select() { 
        if ( is_null(self::$msqlicnct) ) { 
            self::$msqlicnct = new DB_SQL(); 
        } 
        return self::$msqlicnct; 
    } 

    function connect()
    {
        if ($this->link_id == 0) {
            if ($this->pconnect)
                $this->link_id = mysql_pconnect($this->Host, $this->User, $this->Pass);
            else
                $this->link_id = mysql_connect($this->Host, $this->User, $this->Pass);

            if (!$this->link_id)$this->error("connect to host " . $this->Host . " failed");
            if (!mysql_select_db($this->Name, $this->link_id))
                $this->error("cannot use database " . $this->Name);
        }
    }
    function query($sql)
    {
        $this->sql = $sql;
        $this->query_id = mysql_query($sql, $this->link_id);
        if (!$this->query_id)
            $this->error("Invalid SQL");
        $this->count = $this->count + 1;
        return $this->query_id;
    }
    function query_first($query_string)
    {
        $query_id = $this->query($query_string);
        $arr = $this->fetch_array($query_id);
        $this->free_result($query_id);
        return $arr;
    }
    function fetch_array($result = -1)
    {
        if ($result != -1)$this->query_id = $result;
        $arr = mysql_fetch_array($this->query_id);
        return $arr;
    }

    function fetch_assoc($result = -1)
    {
        if ($result != -1)$this->query_id = $result;
        $arr = mysql_fetch_assoc($this->query_id);
        return $arr;
    }

    function free_result($result = -1)
    {
        if ($result != -1)$this->query_id = $result;
        return @mysql_free_result($this->query_id);
    }
    function num_rows($result = -1)
    {
        if ($result != -1)$this->query_id = $result;
        return mysql_num_rows($this->query_id);
    }
    function insert_id()
    {
        return mysql_insert_id($this->link_id);
    }
    function affected_rows()
    {
        return mysql_affected_rows($this->link_id);
    }

    function error($msg)
    {
        echo("<html><body>\n");
        echo("</table></b></i></table></form><blockquote>\n");
        echo("<p>Mysql Script reports an error: $msg</p><br>\n");
        echo("<p>" . mysql_errno() . ": " . mysql_error() . "</p>\n");
        echo("<p>last used query:<br>\n");
        echo("<span style='background-color: #C4C4C4'>" . $this->sql . "</span></p>\n");
        echo("</blockquote></body></html>");
        exit;
    }

    function quote($txt){
		if (get_magic_quotes_gpc()) {
			return $txt;
		} else {
			return addslashes($txt);
		}
	}
}

?>